package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;

import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {


    @Autowired
    private MongoTemplate mongoTemplate;

    //保存点赞、喜欢
    @Override
    public Integer saveComment(Comment comment) {
        mongoTemplate.save(comment);
        switch (comment.getCommentType()) {
            case 1:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setLikeCount(movement.getLikeCount() + 1);
                mongoTemplate.save(movement);
                return movement.getLikeCount();
            }
            case 2:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setCommentCount(movement.getCommentCount() + 1);
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            case 3:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setLoveCount(movement.getLoveCount() + 1);
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }
            case 4:{
                Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
                video.setLikeCount(video.getLikeCount() + 1);
                mongoTemplate.save(video);
                return video.getLikeCount();
            }
            case 5:{
                Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
                video.setCommentCount(video.getCommentCount() + 1);
                mongoTemplate.save(video);
                return video.getCommentCount();
            }
            case 6:{

                Comment comme = mongoTemplate.findById(comment.getPublishId(), Comment.class);
                comme.setLikeCount(comme.getLikeCount() + 1);
                mongoTemplate.save(comme);
                return comme.getLikeCount();

            }
        }

        return 0;
    }
    //取消点赞、喜欢
    @Override
    public Integer removeComment(ObjectId publishId, Long userId, Integer commentType) {
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                        .and("userId").is(userId)
                        .and("commentType").is(commentType)
        );

        mongoTemplate.remove(query,Comment.class);

        switch (commentType) {
            case 1:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setLikeCount(movement.getLikeCount() - 1);
                mongoTemplate.save(movement);
                return movement.getLikeCount();
            }
            case 2:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setCommentCount(movement.getCommentCount()-1);
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            case 3:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setLoveCount(movement.getLoveCount() - 1);
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }
            case 4: {
                Video video = mongoTemplate.findById(publishId, Video.class);
                video.setLikeCount(video.getLikeCount() - 1);
                mongoTemplate.save(video);
                return video.getLikeCount();
            }
            case 5: {
                Video video = mongoTemplate.findById(publishId, Video.class);
                video.setCommentCount(video.getCommentCount() - 1);
                mongoTemplate.save(video);
                return video.getCommentCount();
            }
            case 6: {
                Comment comment = mongoTemplate.findById(publishId, Comment.class);
                comment.setLikeCount(comment.getLikeCount() - 1);
                mongoTemplate.save(comment);
                return comment.getLikeCount();
            }
        }
        return 0;

    }
    //评论分页查询
    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer commentType, Integer pageNum, Integer pageSize) {

        Query query = new Query(
                Criteria.where("commentType").is(commentType)
                        .and("publishId").is(publishId)
        ).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(new Query(Criteria.where("publishId").is(publishId).and("commentType").is(commentType)), Comment.class);

        return new PageBeanVo(pageNum, pageSize, count, comments);
    }
    // 查看对你评论人的信息
    @Override
    public PageBeanVo findUserCommentByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        Query query = new Query(
                Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        return new PageBeanVo(pageNum, pageSize, 0L, commentList);


    }
    //根据id查询视频评论
    @Override
    public Comment findById(ObjectId publishId) {

        return mongoTemplate.findById(publishId, Comment.class);
    }
}
